#!/bin/bash
# 在Ubuntu Linux开发环境下条件编译成Linux程序

BUILD_SERVER_DIR="/home/devbox/project/release/server"  # 更新为你的实际路径
MAIN_GO_PATH="/home/devbox/project/server/main.go"       # 更新为你的实际路径
GO_SERVER_NAME="xingluyi_server"

# 设置Go环境变量以禁用cgo并指定目标操作系统和架构为Linux (x86_64)
export CGO_ENABLED=0
export GOOS=linux
export GOARCH=amd64

# 切换到项目根目录
cd "$(dirname "$MAIN_GO_PATH")" || { echo "Failed to change directory"; exit 1; }

# 编译Go程序，并指定输出文件路径
if go build -o "$BUILD_SERVER_DIR/$GO_SERVER_NAME" "$MAIN_GO_PATH"; then
  echo "编译完成，文件已生成: $BUILD_SERVER_DIR/$GO_SERVER_NAME"
  # 检查编译后的二进制文件是否为Linux可执行文件
  file "$BUILD_SERVER_DIR/$GO_SERVER_NAME"
else
  echo "编译失败，未找到生成的二进制文件: $BUILD_SERVER_DIR/$GO_SERVER_NAME"
  exit 1
fi

# 复制静态资源到发行目录
mkdir -p "$BUILD_SERVER_DIR/static"  # 确保目标目录存在
cp -R static/images "$BUILD_SERVER_DIR/static/"
cp -R static/assets "$BUILD_SERVER_DIR/static/"
cp -R static/docs "$BUILD_SERVER_DIR/static/"
cp -R templates "$BUILD_SERVER_DIR/"

# 提示完成
echo "编译完成，文件已复制到发行目录: $BUILD_SERVER_DIR"